package com.service.school.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.base.BaseDaoI;
import com.model.TCollege;
import com.model.TMajor;
import com.pageModel.DataGrid;
import com.pageModel.Major;
import com.service.school.MajorServiceI;

@Service(value="majorService")
public class MajorServiceImpl implements MajorServiceI {

	private BaseDaoI<TMajor> majorDao;
	
	public BaseDaoI<TMajor> getMajorDao() {
		return majorDao;
	}
	@Autowired
	public void setMajorDao(BaseDaoI<TMajor> majorDao) {
		this.majorDao = majorDao;
	}
	

	private BaseDaoI<TCollege> collegeDao;
	
	public BaseDaoI<TCollege> getCollegeDao() {
		return collegeDao;
	}
	@Autowired
	public void setCollegeDao(BaseDaoI<TCollege> collegeDao) {
		this.collegeDao = collegeDao;
	}

	@Override
	public List<TMajor> getMajorCmb(Major major) {
		String hql="from TMajor t where t.isEnable ='是'";
		List<TMajor> l = majorDao.find(hql);
		return l;
	}
	@Override
	public DataGrid datagrid(Major major) {
		DataGrid dg = new DataGrid();
		String hql="%s from TMajor t %s  where 1=1 ";
		if(major.getMajorId()!=null&&!major.getMajorId().trim().equals("")){
			hql+="and t.majorId like '%"+major.getMajorId().trim()+"%'";
		}
		if(major.getIsEnable()!=null&&!major.getIsEnable().trim().equals("")){
			hql+=" and t.isEnable like '%"+major.getIsEnable().trim()+"%'";
		}
		if(major.getMajorName()!=null&&!major.getMajorName().trim().equals("")){
			hql+=" and t.majorName ='"+major.getMajorName().trim()+"'";
		}
//		if(major.getCollegeName()!=null&&!major.getCollegeName().trim().equals("")){
//			hql+=" and t.collegeID in(select collegeID from TCollege c where c.collegeName like '%"+major.getMajorName().trim()+"%')";
//		}
		List<TMajor> l = majorDao.find(String.format(hql, "", " left join fetch t.TCollege "), major.getPage(), major.getRows());
		List<Major> nl = new ArrayList<Major>();
		if (l != null && l.size() > 0) {
			for (TMajor t : l) {
				Major u = new Major();
				BeanUtils.copyProperties(t, u);
				if (t.getTCollege() != null) {
					u.setCollegeName(t.getTCollege().getCollegeName());
				}
				nl.add(u);
			}
		}
		dg.setTotal(majorDao.count(String.format(hql, "select count(*) ", "")));
		dg.setRows(nl);
		return dg;
	}
	@Override
	public void remove(String ids) {
		String[] nids = ids.split(",");
		String hql = "delete TMajor t where t.id in(";
		for (int i = 0; i < nids.length; i++) {
			if (i > 0) {
				hql += ",";
			}
			hql += "'" + nids[i] + "'";
		}
		hql += ")";
		majorDao.executeHql(hql);
	}
	@Override
	public boolean add(Major major) {
		Long count = majorDao.count("select count(*) from TMajor obj where obj.majorId=?", new Object[] {major.getMajorId()});
		if (count != null && count > 0) {
			 return  false;
		}
		TMajor t=new TMajor();
		t.setId(UUID.randomUUID().toString());
		t.setMajorId(major.getMajorId());
		t.setMajorName(major.getMajorName());
		List<TCollege> collegs = (List<TCollege>)collegeDao.find("from TCollege obj where obj.collegeName=?", new Object[]{major.getCollegeName()});
		if (collegs != null && collegs.size() == 1) {
			TCollege college=collegs.get(0);
			t.setTCollege(college);
		}
		t.setIsEnable("是");
		majorDao.save(t);
		return true;
	}
	@Override
	public void edit(Major major) {
		TMajor t=majorDao.get(TMajor.class,major.getId());
		BeanUtils.copyProperties(major, t);
		List<TCollege> collegs = (List<TCollege>)collegeDao.find("from TCollege obj where obj.collegeName=?", new Object[]{major.getCollegeName()});
		if (collegs != null && collegs.size() == 1) {
			TCollege college=collegs.get(0);
			t.setTCollege(college);
		}
	}

}
